package com.aistar.service.impl;

import com.aistar.mapper.CartDetailsMapper;
import com.aistar.pojo.CartDetails;
import com.aistar.pojo.CartDetailsExample;
import com.aistar.service.CartDetailsService;
import com.aistar.service.CartService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartDetailsServiceImpl implements CartDetailsService {

    @Autowired
    private CartDetailsMapper cartDetailsMapper;

    @Autowired
    private CartService cartService;

    /*添加一个购物详情表*/
    @Override
    public ServerResponse save(CartDetails cartDetails) {
        int rows = cartDetailsMapper.insert(cartDetails);
        if (rows >0){
            return ServerResponse.addSuccess();
        }else {
            return ServerResponse.addFailed();
        }
    }

    //删改
    @Override
    public ServerResponse modifyStatus(CartDetails cartDetails) {
        cartDetails.setCartDetailsStatus(0);
        int rows = cartDetailsMapper.updateByPrimaryKeySelective(cartDetails);
        if (rows>0){
            return ServerResponse.deleteSuccess();
        }else {
            return ServerResponse.deleteFailed();
        }

    }
    //修改 购物车 商品数量的信息
    @Override
    public ServerResponse modify(CartDetails cartDetails) {
        int rows = cartDetailsMapper.updateByPrimaryKey(cartDetails);
        if(rows >0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    /*根据主键查询*/
    @Override
    public ServerResponse getById(Integer id) {
       CartDetails cartDetailsar =  cartDetailsMapper.selectByPrimaryKey(id);

        if(cartDetailsar !=null)
            return ServerResponse.getDataSuccess(cartDetailsar);
        return ServerResponse.getDataFailed("没有该工作经验");
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }


    /*根据购物车id 来查找当前的用户的商品*/
    @Override
    public ServerResponse getAllByCartId(Integer cartId) {
        CartDetailsExample cartDetailsExample = new CartDetailsExample();
        cartDetailsExample.createCriteria().andCartDetailsStatusEqualTo(1)
                .andCartIdEqualTo(cartId);
        List<CartDetails> cartDetails = cartDetailsMapper.selectByExample(cartDetailsExample);
        /*if(cartDetails!=null && cartDetails.size() >0)
            return ServerResponse.getDataSuccess(cartDetails);
        return ServerResponse.getDataFailed("购物车中，没有任何商品");*/
        return ServerResponse.getDataSuccess(cartDetails);
    }

    //购物车添加商品的时候，如果购物车有相同的商品 则不添加
    @Override
    public ServerResponse getOneByCartId(Integer cartId, Integer productId) {
        CartDetailsExample cartDetailsExample = new CartDetailsExample();
        cartDetailsExample.createCriteria().andCartDetailsStatusEqualTo(1)
                                            .andCartIdEqualTo(cartId)
                                            .andProductIdEqualTo(productId);
        List<CartDetails> cartDetails = cartDetailsMapper.selectByExample(cartDetailsExample);
        if(cartDetails!=null && cartDetails.size() >0)
            return ServerResponse.getDataSuccess(cartDetails);
        return ServerResponse.getDataFailed("购物车中，没有任何商品");

    }
}
